#include"dev/time.h"
#include"comm/types.h"
#include"os_cfg.h"
#include"cpu/irq.h"
#include"comm/cpu_instr.h"
#include "core/task.h"
static uint32_t sys_tick;   

void do_handler_time(exception_information* frame)
{
    sys_tick++;
    pic_send_eoi(IRQ0_TIMER);
    //必须在后面
    task_time_tick();
}


static void init_pit()
{
    //1000ms/PIT_OSC_FREQ         周期：10ms
    //每个节拍 reload_count-- reload_count=0 产生中断
    uint32_t reload_count = PIT_OSC_FREQ * OS_TICKS_MS / 1000;      
    outb(PIT_COMMAND_MODE_PORT,PIT_CHANNEL | PIT_LOAD_LOHI | PIT_MOOD3);
    outb(PIT_CHANNEL0_DATA_PORT,reload_count & 0xff);
    outb(PIT_CHANNEL0_DATA_PORT,( reload_count >> 8 ) & 0xff);
    
    irq_install(IRQ0_TIMER,(uint32_t)exception_handler_time);

    //打开中断
    int_open(IRQ0_TIMER);
}

void time_init()
{
    sys_tick=0;
    init_pit();
}